﻿using ERPPublic;
using MediatR;
using Newtonsoft.Json;
using ProductionManagement.Application.Command.PickCommand;
using ProductionManagement.Domain.Dtos;
using ProductionManagement.Domain.ProductionDomain;
using ProductionManagement.Infrstructre.lnterface;
using SqlSugar;
using static ERPPublic.ParseAPI;

namespace ProductionManagement.Application.CommandHandler.PickCommandHandlers
{
    public class FillPickCommandHandlers:IRequestHandler<FillPickCommand,FillPickDTO >
    {

        private readonly IPickRepository _pickRepository;

        private readonly IPickProductRepository _pickProductRepository;

        private readonly ISqlSugarClient db;

        public  FillPickCommandHandlers(IPickRepository pickRepository, IPickProductRepository pickProductRepository, ISqlSugarClient db)
        {
            _pickRepository = pickRepository;
            _pickProductRepository = pickProductRepository;
            this.db = db;
        }

        public async Task<FillPickDTO> Handle(FillPickCommand request, CancellationToken cancellationToken)
        {
            List<Products> products = await GetProductList("", "", 1, 100);
            List<Manpower> manpower = await GetPersonList("", null, null, null, null, 0, 0, 1, 100);
            var p = (from a in await _pickRepository.GetAll() select a).ToList();
            var pickpro = (from b in await _pickProductRepository.GetAll() select b).ToList();
            var pick = (from a in p where a.PickingId ==request.PickId 
                        select new FillPickDTO
                        {
                            PickingId = a.PickingId,
                            PicCollarDate = a.PicCollarDate,
                            PickNum = a.PickNum,
                            PicNumber = a.PicNumber,
                            PicNumTotal = a.PicNumTotal,
                            PicTopic = a.PicTopic,
                            AddName = a.AddName,
                            AddTime = a.AddTime,
                            Office_Id = a.Office_Id,
                            User_Id = a.User_Id,
                            PickProdto = (from c in pickpro 
                                          join f in p on c.PickingId equals f.PicNumber
                                          join d in products on c.ProductId equals d.ProductId
                                          where c.PickingId == a.PicNumber select new PickProDTOs

                            {
                                ManpowerId = c.ManpowerId,
                                ProductId = c.ProductId,
                                OutManager = c.OutManager,
                                OutshipState = c.OutshipState,
                                PickProductDelDate = c.PickProductDelDate,
                                PickingId = c.PickingId,
                                PickProductId = c.PickProductId,
                                PickProductNum = c.PickProductNum,
                                Office_Id = c.Office_Id,
                                User_Id = c.User_Id,
                                AddName = c.AddName,
                                AddTime = c.AddTime,
                                              pName = d.PName,
                                              pNumber = d.PNumber,
                                              pSpec = d.PSpec,
                            }).ToList()
            }).First();


            return pick;
        }




        /// <summary>
        /// 显示产品信息
        /// </summary>
        /// <param name="PName"></param>
        /// <param name="PNumber"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetProductList(string? PName, string? PNumber, int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MSIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTkiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5bCP576OIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.ZjkGj2IxuBzKmF5ZuSyWXQW8TkVqBGLUDkJ86RSweFI";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/erpcp/api/Product/ProductShow?PName=" + PName + "&PNumber=" + PNumber + "&ProductTypeId=" + 0 + "&PageIndex=" + pindex + "&PageSize=" + psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Products>>(postReponse);
            return json.data.list;
        }

        /// <summary>
        /// 显示人员档案
        /// </summary>
        /// <param name="MName"></param>
        /// <param name="Mstart"></param>
        /// <param name="Mend"></param>
        /// <param name="Bstart"></param>
        /// <param name="Bend"></param>
        /// <param name="SectorId"></param>
        /// <param name="PostId"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetPersonList(string? MName, DateTime? Mstart, DateTime? Mend, DateTime? Bstart, DateTime? Bend, int? SectorId, int? PostId, int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MSIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTkiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5bCP576OIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.ZjkGj2IxuBzKmF5ZuSyWXQW8TkVqBGLUDkJ86RSweFI";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/vendor/api/Manpower/GetManpowerList?MName=" + MName + "&Mstart=" + Mstart + "&Mend=" + Mend + "&Bstart=" + Bstart + "&SectorId=&PostId=&Bend=" + Bend + "&Pindex=" + pindex + "&Psize=" + psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Manpower>>(postReponse);
            return json.data.list;
        }


        //产品
        public class Products
        {
            /// <summary>
            /// 产品Id
            /// </summary>
            [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
            public int ProductId { get; set; }
            /// <summary>
            /// 标识
            /// </summary>

            public int? PCharacteristic { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PNumber { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PName { get; set; }
            /// <summary>
            /// 规格型号
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PSpec { get; set; }
            /// <summary>
            /// 单位
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PUnit { get; set; }

            /// <summary>
            /// 产品分类ID
            /// </summary>

            public int ProductTypeId { get; set; }


        }


        /// <summary>
        /// 人员档案
        /// </summary>
        public class Manpower
        {
            /// <summary>
            /// 人员档案id
            /// </summary>
            public int ManpowerId { get; set; }
            /// <summary>
            /// 档案编号
            /// </summary>
            public string? MNumber { get; set; }
            /// <summary>
            /// 登录账号
            /// </summary>
            public string? Users { get; set; }
            /// <summary>
            /// 真实姓名
            /// </summary>
            public string? MName { get; set; }
            /// <summary>
            /// 性别
            /// </summary>
            public bool? MSex { get; set; }
            /// <summary>
            /// 出生日期
            /// </summary>
            public DateTime? MBirthDay { get; set; }
            /// <summary>
            /// 身份证号码
            /// </summary>
            public string? MIdCard { get; set; }
            /// <summary>
            /// 联系电话
            /// </summary>
            public string? MPhone { get; set; }
            /// <summary>
            /// 电子邮箱
            /// </summary>
            public string? MEmail { get; set; }
            /// <summary>
            /// 微信号
            /// </summary>
            public string? MWeChat { get; set; }
            /// <summary>
            /// QQ号
            /// </summary>
            public string? MQQ { get; set; }
            /// <summary>
            /// 紧急联系人
            /// </summary>
            public string? MContact { get; set; }
            /// <summary>
            /// 紧急联系人电话
            /// </summary>
            public string? MCPhone { get; set; }
            /// <summary>
            /// 户口地址
            /// </summary>
            public string? MPermanent { get; set; }
            /// <summary>
            /// 家庭住址
            /// </summary>
            public string? MResidence { get; set; }
            /// <summary>
            /// 证件照
            /// </summary>
            public string? MIdPhoto { get; set; }
            /// <summary>
            /// 入职时间
            /// </summary>
            public DateTime? MEntryDate { get; set; }
            /// <summary>
            /// 所在部门
            /// </summary>
            public int? SectorId { get; set; }
            /// <summary>
            /// 所在岗位
            /// </summary>
            public int? PostId { get; set; }

        }

    }
}
